//
//  ViewController.m
//  RunLoopTest
//
//  Created by yao zhaofeng on 2018/3/28.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSThread *thread;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonClick:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn.tag == 100) {
        //创建
        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task) object:nil];
        [thread start];
        
        //如果不加入这句代码,线程在执行完task方法之后就会立刻被释放,这里写这句代码目的就是保证线程不死,能继续执行其他的任务
        self.thread = thread;
    } else if (btn.tag == 101) {
        //继续
        [self performSelector:@selector(task2) onThread:self.thread withObject:nil waitUntilDone:YES];
    }
}

- (void)task {
    NSLog(@"1------%@",[NSThread currentThread]);
    //创建RunLoop
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    
    //创建timer(这种方法需要手动设置模式)
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
    //
    //    //添加到runloop中,并设置模式
    [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
//        [runLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    
    //开启runloop
    
    [runLoop run];
    
    NSLog(@"2-----%@",[NSThread currentThread]);
}

- (void)task2 {
    NSLog(@"我是另一个任务");
    
    
}

- (void)timeRun {
    NSLog(@"我是定时器方法");
}

@end
